// JavaScript Document
/*******************************************************************
	 *                   编辑器
	 ******************************************************************/ 
//变量
var editor;
var ColorArray=new Array('000000','993300','333300','003300','800000','FF6600','808000','008000','FF0000','FF9900',
	'99CC00','339966','FF00FF','FFCC00','FFFF00','00FF00');  
var current=null;  

	function select_change(ep,pid){
		var value=$('select[name='+ep+']').val();
		if(value=='2'){
			$('select[name='+pid+']').attr('disabled',false);
		}
	}


//率先载入 |iframe编辑
function editframe(iframeid,contentid){  
	//var o=document.getElementById("noteadd");
	editor=document.getElementById(iframeid).contentWindow; 
	editor.document.designMode = 'On';
	editor.document.contentEditable = true; 
	editor.document.body.style.fontSize='14px';  
	editor.document.body.style.fontFamily='微软雅黑';
	var colorpanelid=iframeid+"cp"; 
	document.body.onload = intocolor();
	
}
	function setContentVal(iframeid,contentid){
		var iframecontent=document.getElementById(iframeid).contentWindow.document.getElementsByTagName("body")[0].innerHTML;			
		$('input[name='+contentid+']').attr("value",iframecontent);
	}
	
　　function t(s)
　　{
　　　editor.document.execCommand(s,false,null);
　　} 
	function tcolor(s,color)
　　{
　　　editor.document.execCommand(s,false,color);
　　} 
 
	function intocolor(){  
	var colorTable='';  
	for (i=0;i<4;i++){
	  colorTable=colorTable+'<tr height=12>'; 
	  for (j=0;j<4;j++){  
	 
		colorTable=colorTable+'<td class="colortd" width=11 style="background-color:#'+ColorArray[i*4+j]+'"/>';
	  
	  }  
	  colorTable=colorTable+'</tr>'; 
	}  
	colorTable='<table class="colortable" onclick="doclick()" onmouseover="doOver()">'+colorTable+'</table>';   
	colorpanel.innerHTML=colorTable;
	}  

  
	function doOver() {  
	  if ((event.srcElement.tagName=="TD") && (current!=event.srcElement)) {  
		if (current!=null){
			current.style.backgroundColor = current._background;
		}   
		event.srcElement._background = event.srcElement.style.backgroundColor;
		DisColor.style.backgroundColor = event.srcElement.style.backgroundColor;  
		HexColor.value = event.srcElement.style.backgroundColor.toUpperCase();  
		event.srcElement.style.backgroundColor = "white" ; 
		current = event.srcElement; 
	  }  
	}  
	
	function doclick(){  
	  if (event.srcElement.tagName == "TD")  
	  {  
		var clr = event.srcElement._background;  
		tcolor("ForeColor",clr);
		DisplayClrDlg(false);  
		return clr;  
	  }  
	}  
//选择颜色--------触发事件
	function ShowPanel(){ 
		DisplayClrDlg(true); 
		addEvent(document.body,"mousedown",clickOther);
	}

	function addEvent(obj,eventType,func){
		if(obj.attachEvent){
			obj.attachEvent("on" + eventType,func);
		}else{
			obj.addEventListener(eventType,func,false);
		}
	}
	  
	function clickOther(el){
		thisObj = el.target?el.target:event.srcElement; 
		if(!(thisObj.className=="colortd")){ 
			DisplayClrDlg(false);  
		}
		 
	}
 
	//显示颜色对话框  
	//display 决定显示还是隐藏  
	//显示位置  
	function DisplayClrDlg(display){  
	  var clrPanel = document.getElementById('colorpanel');  
	  if (display){  	   
		var left = document.getElementById("colorwheel").offsetLeft;  
		var top = document.getElementById("colorwheel").offsetHeight+document.getElementById("colorwheel").offsetTop;
		clrPanel.style.pixelLeft = left;  
		clrPanel.style.pixelTop = top;  
		clrPanel.style.display = "block";   
	  }  
	  else{  
		clrPanel.style.display = "none";  
	  }  
	}
	

	
	function note_commit(iframeid,subjectname){
//		var subject=$('input[name=subjectname]').val();	
		var subject=document.getElementsByName(subjectname)[0].value;// 用jquery怎样
		var content=document.getElementById(iframeid).contentWindow.document.getElementsByTagName("body")[0].innerHTML;
		var url= '/notepad/addnote';
		var id=$('#noteid').val();
		var msg_suc="添加成功";
		var msg_fail="添加失败";
		if(id>0){	
			url = '/notepad/editnote'; 
			msg_suc="修改成功";
			msg_fail="修改失败";
		}else{
			id=-1;
		}		 
		$.ajax({
		type : 'post',
		url : url,
		data : {inajax:'1', notesubject:subject, notecontent:content, noteid:id},
		dataType : 'json',
		success : function(data){
			data = $.trim(data);
			switch(data[0]){
				case "1":message(msg_suc, 'notice');break;
				case "2":message(msg_fail, 'notice');break;			
			}			 
			if(wind_is_open){
				loading_hide();
				wind_hide();
			}else{
				window.location.href="/notepad/notelist";
			}
		}
	});
	}
	
	
	
	/**
 * 提示消息
 * @param string msg 消息内容
 * @param string type 类型 有三种选项：[error|info|notice]
 */
function message(msg, type){
	var conf = config['message'];
	if(is_undefined(type)){
		var type = 'info';
	}
	var o = $('#message');
	o.html(msg);
	o.attr('class', type);
	if(message_running){
		clearTimeout(message_settime);
		message_bg();
	}else{
		o.fadeIn(conf.fade_time);
	}
	message_running = true;
	message_settime = setTimeout(function(){
		message_running = false;
		var o = $("#message");
		o.fadeOut(config.message.fade_time);
		o.removeClass(message_type);
	}, conf.display_time);
}

/**
 * 执行动画效果 
 */
function message_bg(){
	message_time = 7;
	_message_bg();
}

/**
 * message_bg() 私有函数
 * 执行动画效果
 */
function _message_bg(){
	if(message_time == 0) return;
	var opacity = 1;
	if(message_time % 2 == 0){
		opacity = 0.5;
	}
	message_time--;
	$('#message').fadeTo(150, opacity, _message_bg);
}

function getCookie(Name) 
{ 
    var search = Name + "=" 
    if(document.cookie.length > 0) 
    { 
        offset = document.cookie.indexOf(search) 
        if(offset != -1) 
        { 
            offset += search.length 
            end = document.cookie.indexOf(";", offset) 
            if(end == -1) end = document.cookie.length 
            return unescape(document.cookie.substring(offset, end)) 
        } 
        else return "" 
    } 
} 
function deleteCookie(name) 
{ 
	var expdate = new Date(); 
	expdate.setTime(expdate.getTime() - (86400 * 1000 * 1)); 
    setCookie(name, "", expdate); 
} 	 